﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kooboo.Commerce
{
    public class PagerModel : IEnumerable<PagerItem>
    {
        public string Url
        {
            get;
            set;
        }

        public int PageCount
        {
            get;
            set;
        }

        public int CurrentPageIndex
        {
            get;
            set;
        }

        public int PageSize
        {
            get;
            set;
        }


        public int RecordCount
        {
            get;
            set;
        }

        public bool HasPrev
        {
            get
            {
                return this.CurrentPageIndex > 1 ;
            }
        }

        public bool HasNext
        {
            get
            {
                return this.CurrentPageIndex < this.PageCount ;
            }
        }

        #region IEnumerable<string> Members

        public IEnumerator<PagerItem> GetEnumerator()
        {
            return this.BuildItems().GetEnumerator();

        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.BuildItems().GetEnumerator();
        }

        #endregion

        List<PagerItem> BuildItems()
        {
            var baseUrl = (this.Url.IndexOf("?") > -1) ?
                 this.Url + "&page={0}&pagesize=" + this.PageSize.ToString() : this.Url + "?page={0}&pagesize=" + this.PageSize.ToString();

            List<PagerItem> list = new List<PagerItem>();

            if (this.PageCount > 1)
            {

                if (this.HasPrev)
                {
                    list.Add(new PagerItem
                    {
                        Url = string.Format(baseUrl, this.CurrentPageIndex - 1),
                        Selected = false,
                        Text = "<"
                    });
                }

                for (var i = 1; i < this.PageCount + 1; i++)
                {


                    list.Add(new PagerItem
                    {
                        Url = string.Format(baseUrl, i),
                        Selected = this.CurrentPageIndex == i,
                        Text = i.ToString()
                    });

                }

                if (this.HasNext)
                {
                    list.Add(new PagerItem
                    {
                        Url = string.Format(baseUrl, this.CurrentPageIndex + 1),
                        Selected = false,
                        Text = ">"
                    });
                }
            }

            return list;
        }
    }

    public class PagerItem
    {
        public bool Selected
        {
            get;
            set;
        }

        public string Text
        {
            get;
            set;
        }

        public string Url
        {
            get;
            set;
        }

    }

    public static class PagerModelBuilder
    {
        public static PagerModel BuildPagerModel<T>(this IQueryable<T> list, string url, int currenPageIndex, int pageSize)
        {
            var model = new PagerModel();
            model.Url = url;
            model.RecordCount = list.Count();
            model.PageSize = pageSize;
            model.CurrentPageIndex = currenPageIndex;
            model.PageCount = (int)Math.Ceiling(model.RecordCount / (double)model.PageSize);

            return model;
        }
    }
}
